#include "../server.hpp"

class EchoServer
{
private:
    TcpServer _server;

private:
    void OnConnected(const PtrConnection &conn)
    {
        DBG_LOG("NEW CONNECTED:%p", conn.get());
    }
    void OnClosed(const PtrConnection &conn)
    {
        DBG_LOG("CLOSE CONNECTED:%p", conn.get());
    }
    // 收到什么发送什么
    void OnMessage(const PtrConnection &conn, Buffer *buf)
    {
        DBG_LOG("%s", buf->ReadPosition());
        conn->Send(buf->ReadPosition(), buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
        // conn->Shutdown();  // 测试接收一次数据就断开连接
    }

public:
    EchoServer(int port) : _server(port)
    {
        _server.SetThreadCount(2);
        _server.EnableInactiveCallback(5);
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }
    void Start() { _server.Start(); }
};
